// pages/shangpiny/shangpiny.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    jfpai: true,
    wode: false,
    shanglb: [],//商品列表
    details:'',//商品详情
    sousuo: '',
    quyuxin:'',
    openid:'',
    bering:'',//跳转编号
    Integral:'',//积分
    biaoti:'',//标题

  },


  back(){
    wx.switchTab({
      url: '../../pages/sangcheng/sangcheng',
    })
  },





//搜索
  onChange(e) {
    var that = this
    console.log(e.detail);
    that.setData({
      sousuo: e.detail,
    });
    if ( e.detail == '') {
      this.commodity()   
    }
  },


  onSearch() {
    // Toast('搜索' + this.data.sousuo);
    wx.request({
      url: app.globalData.interface+'/newVersion/API_2002_getOneGoods',
      method: 'GET',
      data: {
        OPENID: this.data.openid,
        SHAANXICITY: this.data.quyuxin,
        KEYWORD:this.data.sousuo,
        OPEN_ID: this.data.openid, //用户OPEN_ID
        session_key:this.data.session_key,
        OPEN_UUID:this.data.jiami
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res);
        console.log(res.data.allGoods);

        this.setData({
          shanglb:res.data.allGoods
        })

      }
    })
  },
  onClick() {
    // Toast('搜索' + this.data.sousuo);

    wx.request({
      url: app.globalData.interface+'/newVersion/API_2002_getOneGoods',
      method: 'GET',
      data: {
        OPENID: this.data.openid,
        SHAANXICITY: this.data.quyuxin,
        KEYWORD:this.data.sousuo,
        OPEN_ID: this.data.openid, //用户OPEN_ID
        session_key:this.data.session_key,
        OPEN_UUID:this.data.jiami
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res);
        console.log(res.data.allGoods);

        this.setData({
          shanglb:res.data.allGoods
        })

      }
    })

  },

  //详情
  danxiang(e){
    console.log(e.currentTarget.dataset.item);
    this.setData({
      details:e.currentTarget.dataset.item
    })
    
    wx.navigateTo({
      url: '../../pages/spxiangq/spxiangq?data=' + JSON.stringify(this.data.details)
      })
  },


  //商品排序
  shangxia() {

    var that = this
    console.log(11);
    const a = that.data.shanglb
    if (that.data.jfpai == false) {
      const g = a.sort((a, b) => {
        return b.GOODS_INTEGRAL - a.GOODS_INTEGRAL
      })
      console.log('llllllllllll', g);
      that.setData({
        shanglb: g
      })
    } else {
      
      const j = a.sort((a, b) => {
        return a.GOODS_INTEGRAL - b.GOODS_INTEGRAL
      })
      console.log('llllllllllll', j);
      that.setData({
        shanglb: j
      })
    }
    this.setData({
      jfpai: !this.data.jfpai,
    })
    console.log(this.data.jfpai);
  },

  myTrade(){
   const list = this.data.shanglb.filter((item)=>{
     return  item.GOODS_INTEGRAL <= this.data.Integral
    })
    this.setData({
      shanglb:list,
      wode:!this.data.wode,
    })
    console.log(this.data.wode);

    if (this.data.wode==false) {
      this.commodity()
    }
  },

  
  commodity(){
    wx.request({
      url: app.globalData.interface+'/newVersion/API_2001_getGoodsList',
      method: 'GET',
      data: {
        OPENID: this.data.openid,
        SHAANXICITY: this.data.quyuxin,
        GOODS_CATEGORY_ID: this.data.bering,
        OPEN_ID: this.data.openid, //用户OPEN_ID
        session_key:this.data.session_key,
        OPEN_UUID:this.data.jiami
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res);
        console.log(res.data.data[0].goods);
        this.setData({
          shanglb:res.data.data[0].goods,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(e) {
    console.log(JSON.parse(e.data));
    var shangpin = JSON.parse(e.data)
    console.log(shangpin);
      this.setData({
        bering:shangpin.GOODS_CATEGORY_ID,
        biaoti:shangpin.CATEGORY_NAME,
        // shanglb: shangpin.goods
      })
      console.log(this.data.bering);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {    
    this.setData({
      quyuxin: app.globalData.quyuxin,
      openid: app.globalData.openid,
      Integral: app.globalData.Integral,
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      openid: app.globalData.openid,
      session_key: app.globalData.session_key,
    });
    console.log(this.data.quyuxin);
    console.log(this.data.openid);
    console.log(this.data.Integral);

    if ( this.data.bering != '') {
      this.commodity()
    } 
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})